<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<?php
if (count($_POST['units']) < 1) {
	header('Location: Vocabulary.php');
}
?>

<head>
	<title>Greek Vocabulary</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="flashcard.css" rel="stylesheet" type="text/css">
</head>

<?php

try {
	$db = new PDO('sqlite:exercise.db3');
	$words = array();
	foreach($_POST['units'] as $bkunit) {
		list($book, $unit) = explode('.', $bkunit);
		$q = $db->query("SELECT word, verbum, type, definition FROM De_Bello_Gallico WHERE liber=$liber AND caput=$caput;");
		$words = array_merge($words, $q->fetchAll(PDO::FETCH_NUM));
	}
	shuffle($words);
	
	$db = NULL;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>
<script type="text/javascript">
<?php
print "var words = new Array(".count($words).");\n";
for($i = 0; $i < count($words); $i++) {
	$tmp = implode("', '", $words[$i]);
	$tmp = str_replace("<b>", '<span class="vl">', $tmp);
	$tmp = str_replace("</b>", '</span>', $tmp);
	$tmp = str_replace('[', '<br />[', $tmp);
	$tmp = str_replace(']', ']<br />', $tmp);
	//$tmp = str_replace('(W.', '<span class="wl">(W.', $tmp);
	//$tmp = str_replace(')', ')</span>', $tmp);
	$tmp = preg_replace('/(\(W\.[^)]+\))/', "<span class=\"wl\">$1</span>", $tmp);
	$tmp = str_replace('<br /><br />', '<br />', $tmp);


	print "words[$i] = ['$tmp'];\n";
}
?>

var shown = 0;

function updateVoc() {
	var i = Math.floor(shown / 4);

	var j = shown % 4;
	for(n = 0; n < 4; n++) {
		if (j == 0) {
			document.getElementById('v0').style.display = 'block';
		} else {
			document.getElementById('v0').style.display = 'none';
		}
		
		if (n <= j) {
			document.getElementById('v' + n).innerHTML = words[i][n];
		} else {
			document.getElementById('v' + n).innerHTML = "";
		}
	}
}

function next() {
	shown++;
	if (shown > words.length * 4 - 1) {
		shown = 0;
		alert('All done!');
	}
	updateVoc();
}

function prev() {
	shown--;
	if (shown < 0) {
		shown = words.length * 4 - 1;
		alert('All done!');
	}
	updateVoc();
}

</script>

<body onload="updateVoc()">

<h1 style="text-align:center">De Bello Gallico - <?php echo "Liber $liber, Caput $caput"; ?></h1>

<div class="center">

<div id="v0" class="v0"></div>
<div id="v1" class="v1"></div>
<div id="v2" class="v2"></div>
<div id="v3" class="v3"></div>

<a href="#" class="button" id="btnprev" onclick="prev()" accesskey="p">&lt;&lt;</a>
<a href="#" class="button" id="btnnext" onclick="next()" accesskey="n">&gt;&gt;</a>



</div>
</body>
</html>
